package com.angel.resourceBundle.containers.propertyValues.impl;

import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.MappedSuperclass;
import javax.persistence.OneToMany;

import org.apache.commons.collections15.CollectionUtils;
import org.apache.commons.collections15.Transformer;

import com.angel.architecture.persistence.base.PersistentObject;
import com.angel.resourceBundle.containers.propertyValues.PropertyValueContainer;
import com.angel.resourceBundle.propertyValue.PropertyValue;
import com.angel.resourceBundle.propertyValue.impl.SimplePropertyValue;


/**
 *
 * @author William
 * @since 20/April/2009
 */
@MappedSuperclass
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class SimplePropertyValueContainer extends PersistentObject implements Comparable<Object>, PropertyValueContainer{

	private static final long serialVersionUID = 2294871627749558701L;

	@OneToMany
	@JoinColumn(name = "simplePropertyValueContainerId")
	private Collection<SimplePropertyValue> propertiesValues;

    public abstract void setOwner(Object owner);

    public abstract Object getOwner();

    public SimplePropertyValueContainer() {
        super();
        this.propertiesValues = new ArrayList<SimplePropertyValue>();
    }

    public <T extends PropertyValue> SimplePropertyValueContainer(Object owner) {
        super();
        this.propertiesValues = new ArrayList<SimplePropertyValue>();
        this.setOwner(owner);
    }

    public <T extends SimplePropertyValue> SimplePropertyValueContainer(Collection<T> propertiesValues) {
        super();
        this.propertiesValues = new ArrayList<SimplePropertyValue>();
        this.propertiesValues.addAll(propertiesValues);
    }

    public <T extends SimplePropertyValue> SimplePropertyValueContainer(Object owner, Collection<T> propertiesValues) {
        super();
        this.propertiesValues = new ArrayList<SimplePropertyValue>();
        this.propertiesValues.addAll(propertiesValues);
        this.setOwner(owner);
    }

    public String getTextFor(String languageName){
        String text = null;
        for(PropertyValue propertyValue : this.propertiesValues){
        	if(propertyValue.isForLanguage(languageName)){
        		text = propertyValue.getText();
        		break;
        	}
        }
        return text;
    }

    public <T extends PropertyValue> Boolean containsPropertyValue(T propertyValue){
    	return this.propertiesValues.contains(propertyValue);
    }

    public <T extends PropertyValue> void removePropertyValue(T propertyValue){
    	this.propertiesValues.remove(propertyValue);
    }

    public <T extends PropertyValue> void addPropertyValue(T propertyValue){
    	this.propertiesValues.add((SimplePropertyValue) propertyValue);
    }

	/**
	 * @return the propertiesValues
	 */
	public Collection<PropertyValue> getPropertiesValues() {
		return CollectionUtils.transformedCollection(propertiesValues,
				new Transformer<SimplePropertyValue, PropertyValue>(){
			public PropertyValue transform(SimplePropertyValue simplePropertyValue) {
				return simplePropertyValue;
			}

		});
	}

	@SuppressWarnings("unchecked")
	public <T extends PropertyValue> void addAllPropertiesValues(Collection<T> propertiesValues){
		this.propertiesValues.addAll((Collection<? extends SimplePropertyValue>) propertiesValues);
	}

	public Collection<String> getPropertiesValuesText() {
		Collection<String> propertiesValuesText = CollectionUtils.transformedCollection(
						this.getPropertiesValues(),
						new Transformer<PropertyValue, String>(){
							public String transform(PropertyValue propertyValue) {
								return propertyValue.getText();
							}
						}
			);
		return propertiesValuesText;
	}

	public Long getPropertiesValuesSize(){
		return Long.valueOf(String.valueOf(this.propertiesValues.size()));
	}
}